package priv.pront.code.national.acwing.search.bfs;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @Description:
 * @Author: pront
 * @Time:2023-05-23 19:28
 */
public class Acwing188_武士风度的牛 {

    static int x, y, startx, starty;
    static char[][] g;
    static boolean[][] st;
    static int[] dx = {-2,-1,1,2,2,1,-1,-2}, dy = {1,2,2,1,-1,-2,-2,-1};
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        y = scanner.nextInt(); x = scanner.nextInt();
        g = new char[x][y];
        st = new boolean[x][y];
        for(int i = 0; i < x; i++){
            String s = scanner.next();
            for(int j = 0; j < s.length(); j++){
                if(s.charAt(j) == 'K'){
                    startx = i;
                    starty = j;
                }
                g[i][j] = s.charAt(j);
            }
        }
        bfs();
    }

    static void bfs(){
        Queue<int[]> queue = new LinkedList<>();
        st[startx][starty] = true;
        queue.add(new int[]{startx, starty, 0});
        while(!queue.isEmpty()){
            int[] cur = queue.poll();
            if(g[cur[0]][cur[1]] == 'H'){
                System.out.println(cur[2]);
                break;
            }
            for(int i = 0; i < 8; i++){
                int nx = cur[0] + dx[i] , ny = cur[1] + dy[i];
                if(nx < 0 || nx >= x || ny < 0 || ny >= y) continue;
                if(g[nx][ny] == '*' || st[nx][ny]) continue;
                queue.add(new int[]{nx, ny, cur[2] + 1});
                st[nx][ny] = true;
            }
        }
    }
}
